package com.homework.config;

import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.context.annotation.Configuration;
import org.springframework.stereotype.Component;

@Component
public class MyApplicationContext implements ApplicationContextAware {

    private static ApplicationContext context;

    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        MyApplicationContext.context = applicationContext;
    }

    public static ApplicationContext getContext(){
        return context;
    }

    public static Object getBean(String beanName){
        if (context == null){
            return null;
        }
        return context.getBean(beanName);
    }

    public static <T> T getBean(Class<T> clazz){
        if (context == null){
            return null;
        }
        return context.getBean(clazz);
    }

    public static String[] getBeanDefinitionNames(){
        return context.getBeanDefinitionNames();
    }
}
